package net.seabookchen.controller;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.seabookchen.dao.MoodDao;
import net.seabookchen.pojo.Mood;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.datastore.EntityNotFoundException;

@Controller
public class MoodController extends CommonController<Mood> {
	private static final Logger log = Logger.getLogger(MoodController.class.getName());
	private MoodDao mDao;
	
	public MoodController() {
		if (mDao == null) mDao = new MoodDao(Mood.class); 
	}
	
	@RequestMapping(value ="/mood", method=RequestMethod.POST)
	public String postMood(HttpServletRequest request, HttpServletResponse resp, Model model) {
		String moodStr = request.getParameter("mood");
		String moodType = request.getParameter("moodType");
		Mood mood = new Mood(moodStr);
		mood.setType(Mood.getMoodType(moodType));
		mDao.put(mood);
		populateModel(model, mood, RequestMethod.POST + "-Mood-" + mood.getId() + " successfully!");
		log.info("Post Mood: " + mood);
		return "showMoods";
	}
	
	@RequestMapping(value ="/allmoods", method=RequestMethod.GET)
	public String listMoods(HttpServletRequest request, HttpServletResponse resp, Model model) {
		List<Mood> moods =  mDao.listMoods();
		//model.addAttribute("action", "List All Moods");
		model.addAttribute("entities", moods);
		log.info("List All Moods size: " + moods.size());
		return "showMoods";
	}
	
	@RequestMapping(value ="/allmoods/{index}", method=RequestMethod.GET)
	public String listMoods(HttpServletRequest request, HttpServletResponse resp, @PathVariable String index, Model model) {
		List<Mood> moods =  mDao.listMoods(Integer.valueOf(index));
		//model.addAttribute("action", "List All Moods");
		model.addAttribute("entities", moods);
		log.info("List All Moods size: " + moods.size());
		return "showMoods";
	}
	
	@RequestMapping(value ="/mood/{id}", method=RequestMethod.GET)
	public String getMood(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		Mood mood = null;
		try {
			mood = mDao.get(Long.parseLong(id));
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, mood, RequestMethod.GET + "-Mood-" +id);
		log.info("GET Mood: " + mood);
		return "showMoods";
	}
	
	@RequestMapping(value ="/mood/{id}", method=RequestMethod.PUT)
	public String putMood(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		String moodStr = request.getParameter("mood");
		String moodType = request.getParameter("moodType");
		Mood mood = null;
		try {
			mood = mDao.get(Long.parseLong(id));
			mood.setType(Mood.getMoodType(moodType));
			mood.setMoods(moodStr);
			mDao.put(mood);
		} catch (NumberFormatException e) {
			log.severe(e.getMessage());
		} catch (EntityNotFoundException e) {
			log.severe(e.getMessage());
		}
		populateModel(model, mood, RequestMethod.GET + "-Mood-" +id);
		log.info("Put Mood: " + mood);
		return "showMoods";
	}
	
	@RequestMapping(value ="/mood/{id}", method=RequestMethod.DELETE)
	public String deleteMood(HttpServletRequest request, HttpServletResponse resp, @PathVariable String id, Model model) {
		mDao.delete(Long.parseLong(id));
		model.addAttribute("action", RequestMethod.DELETE + "-Mood-" +id);
		log.info("Delete Mood: " + id);
		return "showMoods";
	}
	
}
